var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")),
  a = require("../../@babel/runtime/helpers/asyncToGenerator"),
  e = require("../../utils/login"),
  i = require("../../utils/config"),
  n = getApp();
Page({
  data: {
    isShowBar: !1,
    isShowTips: !0,
    isRender: !1,
    soldOut: 0,
    isPause: !1,
    motto: "首页",
    bgImage1: "".concat(i.imgPrefix, "/static/assets/index-img/index_01_4.png"),
    bgImage2: "".concat(i.imgPrefix, "/static/assets/index-img/index_02_3.png"),
    bgImage3: "".concat(i.imgPrefix, "/static/assets/index-img/index_03_3.png"),
    ruleImage: "".concat(i.imgPrefix, "/static/assets/index_bg_rule.png"),
    tipsImage: "".concat(i.imgPrefix, "/static/assets/index_bg_up.png"),
    musicImage: "".concat(i.imgPrefix, "/static/assets/btn_music.png"),
    kfBirdImage: "".concat(i.imgPrefix, "/static/assets/bird/kf_bird_0.png"),
    logo1: "".concat(i.imgPrefix, "/static/assets/index-img/logo1.png"),
    logo2: "".concat(i.imgPrefix, "/static/assets/index-img/logo2.png"),
    logo3: "".concat(i.imgPrefix, "/static/assets/index-img/logo4.png"),
    canvasBoottom: 0,
    navBar: {
      statusBarHeight: 0,
      navigationBarHeight: 0
    }
  },
  onLoad: function () {},
  onReady: function () {
    this.canvasObj = null;
    var t = wx.getStorageSync("statusBarHeight"),
      a = wx.getStorageSync("navigationBarHeight");
    this.windowHeight = wx.getSystemInfoSync().windowHeight;
    var e = {
      statusBarHeight: t,
      navigationBarHeight: a
    };
    console.log("navBar:", e), n.globalData.bgAudio && n.globalData.bgAudio.play(), this.setData({
      navBar: e,
      soldOut: n.globalData.soldOut,
      isRender: !0
    })
  },
  onShow: function () {
    if (n.globalData.isShoHide) return n.globalData.isShoHide = !1;
    this.count = 0, this.initKf()
  },
  onHide: function () {
    if (n.globalData.isShoHide) return !1;
    if (this.timer && this.canvasObj) {
      clearInterval(this.timer);
      var t = this.canvasObj,
        a = t.width,
        e = t.height;
      t.ctx.clearRect(0, 0, a, e), this.count = 0
    }
  },
  loadImg: function (t, a) {
    return new Promise((function (e, i) {
      var n = t.createImage();
      n.onload = n.onerror = function () {
        e(n)
      }, n.src = a
    }))
  },
  createCanvas: function () {
    var t = this;
    return new Promise((function (a, e) {
      wx.createSelectorQuery().select("#kf-bird").fields({
        node: !0,
        size: !0
      }).exec((function (i) {
        i[0] || e();
        var n = i[0].node,
          s = n.getContext("2d");
        n.width = 2 * i[0].width, n.height = 2 * i[0].height;
        var r = n.width,
          o = n.height;
        t.canvasObj = {
          canvas: n,
          ctx: s,
          width: r,
          height: o
        }, a()
      }))
    }))
  },
  initKf: function () {
    var e = this;
    return a(t.default.mark((function a() {
      var n, s;
      return t.default.wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            if (e.canvasObj) {
              t.next = 4;
              break
            }
            return console.log("create kf!"), t.next = 4, e.createCanvas();
          case 4:
            for (n = [], s = 0; s <= 36; s++) n.push(e.loadImg(e.canvasObj.canvas, "".concat(i.imgPrefix, "/static/assets/bird/kf_bird_").concat(s, ".png")));
            return t.next = 8, Promise.all(n);
          case 8:
            e.kfImageArr = t.sent, e.count = 0, e.kfLen = e.kfImageArr.length, e.timer = setInterval(e.playKf, 1e3 / 12);
          case 12:
          case "end":
            return t.stop()
        }
      }), a)
    })))()
  },
  playKf: function () {
    if (this.count += 1, !this.canvasObj) return !1;
    var t = this.canvasObj,
      a = t.width,
      e = t.height,
      i = t.ctx;
    this.count >= this.kfLen && (this.count = 0);
    var n = this.kfImageArr[this.count],
      s = n.width,
      r = n.height;
    i.clearRect(0, 0, a, e), i.drawImage(this.kfImageArr[this.count], 0, 0, s, r, 0, 0, a, e)
  },
  musicHandler: function () {
    n.globalData.bgAudio && !this.data.isPause ? n.globalData.bgAudio && n.globalData.bgAudio.stop() : n.globalData.bgAudio && n.globalData.bgAudio.play(), this.setData({
      isPause: !this.data.isPause
    })
  },
  scrollHandler: function (t) {
    var a = t.detail,
      e = a.scrollTop,
      i = a.scrollHeight;
    e >= 80 && !this.data.isShowBar && this.setData({
      isShowBar: !0
    }), e < 80 && this.data.isShowBar && this.setData({
      isShowBar: !1
    }), i - e <= this.windowHeight + 50 && this.data.isShowTips && this.setData({
      isShowTips: !1
    }), i - e > this.windowHeight + 50 && !this.data.isShowTips && this.setData({
      isShowTips: !0
    })
  },
  bindLandViewHandler: function () {
    return a(t.default.mark((function a() {
      return t.default.wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            return t.next = 2, (0, e.wxlogin)();
          case 2:
            if (t.sent) {
              t.next = 5;
              break
            }
            return t.abrupt("return", !1);
          case 5:
            "/pages/land/index2", wx.switchTab({
              url: "/pages/land/index2"
            });
          case 7:
          case "end":
            return t.stop()
        }
      }), a)
    })))()
  },
  gotoLiveHandler: function () {
    n.globalData.livePageFrom = "index";
    wx.switchTab({
      url: "/pages/live/index"
    })
  },
  openRulesHandler: function () {
    wx.navigateTo({
      url: "./rules/index"
    })
  }
});